<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ActivitiesPostRequest;

class NotesController extends Controller
{
   /**
     * 展示志愿者手记内容列表
     *
     * @return 视图
     */
    public function getIndex(request $request)
    {
      //获取提交参数
        $strip = $request->input('strip','10');
        $keywords = $request->input('keywords');

        if(isset($keywords)){
        
            $datas = DB::table('activities')->where('sort','0')->where('title','like','%'.$keywords.'%')->paginate($strip);
        }else{
            
            //查询数据库
             $datas = DB::table('activities')->where('sort','0')->orderBy('time', 'desc')->paginate($strip); 
        }
        $status = ['禁止','开启'];
        return view('Admin.notes.index',['datas'=>$datas,'pages'=>$request->all(),'status'=>$status]);  
    }
    /**
     * 展示志愿者手记内容添加视图
     *
     * @return 视图
     */
    public function getAdd()
    {
        return view('Admin.notes.add');
    }

    /**
     * 执行数据入库
     *
     * @return 受影响行数
     */
    public function postInsert(ActivitiesPostRequest $request)
    {
        //获取请求参数
        $data = $request->except('_token');
        $data['pic'] = $this->upload($request);
        $data['time'] = time();
        $data['sort'] = 0;
        
        //执行数据入库
        $num = DB::table('activities')->insertGetId($data);
       //判断数据是否录入成功
        if($num)
        {
            return redirect('/admin/notes/index')->with('success', '手记内容添加成功');
        }else{
            return back()->with('error', '手记内容添加失败');
        }
    }

    //文件上传
    public function upload($request)
    {
        $name = md5(time()+rand(1,9999));

        //获取文件后缀名
        $su = $request->file('pic')->getClientOriginalExtension();

        //移动文件
        if ($request->hasFile('pic')) {
            
            $request->file('pic')->move('./public/uploads', $name.'.'.$su);
            return '/public/uploads/'.$name.'.'.$su;
        }
    }

    /**
     * 展示后台项目内容
     *
     * @return 后台项目内容视图
     */
    public function getInfo($id)
    {
        //查询分类数据
        $datas = DB::table('activities')->where('id',$id)->first();
        
       return view('Admin.notes.info',['datas'=>$datas]);
    }


     /**
     * 执行删除操作
     *
     * @return 受影响行数
     */
    public function getDel($id)
    {
        
        //执行删除操作
        $num = DB::table('activities')->where('id',$id)->delete();
       
       if($num){
            return redirect('/admin/notes/index')->with('success', '手记内容删除成功');
       }else{
            return redirect('/admin/notes/index')->with('error', '手记内容添加成功');
       }
       
    }

    /**
     * 展示后台项目内容信息修改
     *
     * @return object
     */
    public function getEdit($id)
    {
        //查询数据
        $datas = DB::table('activities')->where('id',$id)->first();

        return view('Admin.notes.edit',['datas'=>$datas]);
    }

    /**
     * 执行修改操作
     *
     * @return 受影响行数
     */
    public function postUpdate(request $request)
    {
        //获取需要的提交数据
        $data = $request->except(['_token','id']);

        //调用方法执行文件上传
       $data['pic'] = $this->upload($request);
       
       //执行修改
        $res = DB::table('activities')->where('id',$request->input('id'))->update($data);

        if($res){
            return redirect('/admin/notes/index')->with('success', '手记内容修改成功');

        }else{
            return redirect('/admin/notes/index')->with('error', '手记内容修改失败');

        }
    }

}
